home *** CD-ROM | disk | FTP | other *** search
- Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!www!www!not-for-mail
- From: cdh@www.oracorp.com (Douglas Harper)
- Newsgroups: comp.lang.c
- Subject: Re: Loop Invariant
- Date: 9 Feb 1996 16:52:08 -0500
- Organization: Odyssey Research Associates, Inc., Ithaca NY
- Sender: cdh@oracorp.com
- Message-ID: <4fgfm8$eeb@www.oracorp.com>
- NNTP-Posting-Host: www.oracorp.com
-
-
- Jonas J. Schlein (schlein@gl.umbc.edu) writes:
- >Jason Klinke <jklinke@uvaix.uvic.ca> wrote:
- >|> I'm having difficulty coming up with a loop invariant that I can prove, for
- >|> the following code which computes the sum of the integers in the array
- >|> A[0..n-1] :
- >|>
- >|> -----------
- >|> sum = 0;
- >|> for (i = 0; i < n; i++)
- >|> sum = sum + A[i];
- >|> -----------
- >|>
- >|> Any help with this invariant and its proof is appreciated.
- >
- >Do it by induction...Verify that it works for the sum of the integers in
- >am empty array (or an array with only 1 element). Then assume it works
- >for any array with n-1 elements and from that prove that this implies
- >it also works for an array with n elements.
- >
- >Basically this idea is called mathematical induction. There is a weak and
- >strong form. I can't believe your teacher did not tell you about this
- >since that is obviously what is probably intended.
-
- To expand on this, the invariant that Jason Klinke asked for is
- essentially the induction hypothesis. If you are assuming that n > 0,
- this invariant works (sorry about the ASCII notation):
-
- _ i - 1
- sum = > A[j] /\ (0 <= i <= n)
- - j = 0
-
- If you can't assume that n > 0, this invariant works:
-
- _ i - 1
- sum = > A[j] /\ (0 <= i <= max(0, n))
- - j = 0
-
- My company has developed the Penelope verification system for proving
- the correctness of Ada programs, so just for fun I translated the loop
- to Ada and verified it under both assumptions. (Because the semantics
- of C and Ada for loops is different, I rewrote the for loop as a while
- loop.) Here's the program text under the assumption that n > 0:
-
- --------------------------------- [Cut here] ---------------------------------
- with p;
- --| with trait t ;
- procedure foo(a : in p.intarray; n : in integer; sum : in out integer)
- --| where
- --| in n>0;
- --| out sum = sigma(0, n-1, a);
- --| end where;
- --! VC Status: proved
- --! BY instantiation of sigma_base as rewrite rule
- --! BY limited simplification, simplification
- --! BY synthesis of TRUE
-
-
- is
- --! PRECONDITION = (0 = sigma(0, -1, a) and 0 <= n)
- i : integer := 0;
-
- begin
- sum := 0;
- --! PRECONDITION = (sum = sigma(0, i - 1, a) and (0 <= i and i <= n))
- --! VC Status: proved
- --! BY instantiation of sigma_step with (a for a, i for n)
- --! rewriting left to right
- --! BY simplification
- --! BY synthesis of TRUE
-
- while (i<n) loop
- --| invariant sum = sigma(0, i-1, a) and (0<=i and i<=n);
- sum := sum+a(i);
- i := i+1;
- end loop;
- end foo;
- --
- Douglas Harper | 1976: lounge lizard
- +1 (607) 277-2020 | 1986: couch potato
- cdh@oracorp.com | 1996: monitor lizard (term swiped from Arden Tohill)
-